package com.rng.dateless;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by tkoh on 27.10.2014.
 */
public class NewEvent extends Activity implements View.OnClickListener {
    String recurrences = "7770,6662,1001";
    CustomAdapter adapter;
    ListView listView;
    DBHelper dbHelper;
    Button datePicker;
    Button doneButton;
    Button addPeriodicityButton;
    TextView dayLabel;
    TextView monthLabel;
    TextView yearLabel;
    EditText description;
    Date date;
    int day;
    int month;
    int year;
    @Override
    public void  onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.new_event);
        listView = (ListView)findViewById(R.id.recurrenceListView);
        dbHelper = new DBHelper(this);
        description = (EditText)findViewById(R.id.description);
        doneButton = (Button)findViewById(R.id.doneButton);
        addPeriodicityButton = (Button)findViewById(R.id.addPeriodicityButton);
        datePicker = (Button)findViewById(R.id.datePicker);
        doneButton.setOnClickListener(this);
        addPeriodicityButton.setOnClickListener(this);
        dayLabel = (TextView)findViewById(R.id.day_text);
        monthLabel = (TextView)findViewById(R.id.month_text);
        yearLabel = (TextView)findViewById(R.id.year_text);
        datePicker.setOnClickListener(this);
        Calendar calendar = new GregorianCalendar();
        date  = new Date();
        refreshDate(date);
    }
    @Override
    public void  onResume () {
        super.onResume();
//        SQLiteDatabase db = dbHelper.getReadableDatabase();
//        String query = "SELECT "+DBHelper.COL_RECURRENCE + "FROM " + DBHelper.TABLE_NAME+" WHERE id = ";
//
//        Cursor c = db.query(true, dbHelper.TABLE_NAME, new String[] {"ds"},null, null, null, null, null, null);
//        if (c.moveToFirst()){
//            do {
//
//            }
//            while (c.moveToNext());
//        }
        adapter = new CustomAdapter(this,R.layout.recurrence_list_row, recurrences.split(","));
        listView.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_list:
                Intent intent = new Intent(getApplicationContext(), ListActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.datePicker:
                DialogFragment dialog = new DatePickerFragment();
                dialog.show(getFragmentManager(), "date picker!");
                break;
            case R.id.doneButton:
                //saving
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues cv = new ContentValues();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                cv.put(DBHelper.COL_DATE, dateFormat.format(date));
                cv.put(DBHelper.COL_DESCRIPTION, description.getText().toString());
                db.insert(DBHelper.TABLE_NAME, null, cv);
                db.close();
                dbHelper.close();
                finish();
                break;
            case R.id.addPeriodicityButton:
                NumberPickerDialog numberPickerDialog = new NumberPickerDialog();
                numberPickerDialog.show(getFragmentManager(), "number picker dialog");
            default:
        }
    }
    public void onUserSelectDate(java.util.Date date) {
        refreshDate(date);
    }
    public void onUserAddRecurrence(int type, int value) {
        adapter.add(Integer.toString(value)+Integer.toString(type));
        adapter.notifyDataSetChanged();
    }
    private void refreshDate(java.util.Date date)
    {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        year = calendar.get(Calendar.YEAR);
        dayLabel.setText(Integer.toString(day));
        monthLabel.setText(Integer.toString(month));
        yearLabel.setText(Integer.toString(year));
        this.date = date;
    }
    private static class CustomAdapter extends ArrayAdapter<String> {
        public CustomAdapter(Context context, int layout,
                             String[] items) {
//Call through to ArrayAdapter implementation
            super(context, layout, items);
        }
        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            View row = convertView;
//Inflate a new row if one isn't recycled
            if(row == null) {
                row = LayoutInflater.from(getContext())
                        .inflate(R.layout.recurrence_list_row, parent, false);
            }
            String element = getItem(position);
            String type = element.substring(element.length()-1);
            String value = element.substring(0, element.length()-1);
            TextView typeView = (TextView)row.findViewById(R.id.type);
            TextView valueView = (TextView)row.findViewById(R.id.value);

            typeView.setText(type);
            valueView.setText(value);
            return row;
        }
    }
}
